BuildingSelected .equ appbackupscreen + 30

#include "Macros.asm"

BasicBuildings .equ 1
ResearchBuildings .equ 2





AddObject:


 BIT Select_Mode, (IY + asm_flag1)
 jr z, _
 RET

_

 ld e, BasicBuildings

Select_Buildings_Submenus:

 ld hl, Build_Basic_Buildings
 

 ld a,e
 cp BasicBuildings
 call z, Draw_Icon_Menu
 ld hl, Build_Research_Buildings
 ld a,e
 
 cp ResearchBuildings
 call z, Draw_Icon_Menu
 ld a,e
 cp BasicBuildings
 jr z, Select_Buildings_Submenus 
 ret


AddObjectToGame:  

 BIT Object_Ready_To_Add, (IY + asm_flag1)
 jr nz, _

 SET Object_Ready_To_Add, (IY + asm_flag1)
 CALL Rotate_Select_Mode
 ld a,c
 ld (BuildingSelected),a
 RET




_

 RES Object_Ready_To_Add, (IY + asm_flag1)
 CALL Rotate_Select_Mode
 ld a, (BuildingCount)

 ld h,0
 ld l,a
 
 ld d,h
 ld e,l
 call multiply_de_by_11

 ex de, hl
 ld hl, (Objects_Address)
 
 add hl, de
 push hl
 
 ld hl,(MapX)
 
 ld a, (Select_Cursor_X)
 ld d, 0
 ld e,a
 add hl, de
 ex de, hl
 pop hl

 ld (hl), e
 inc hl
 ld (hl),d
 inc hl

 push hl
 ld hl, (MapY)


 ld a, (Select_Cursor_Y)
 ld d, 0
 ld e,a
 add hl, de
 ex de, hl
 pop hl


 ld (hl), e

 inc hl
 ld (hl),d
 inc hl
 

 ld a, (BuildingSelected)

Continue:


 ld (hl), a
 push af

 push hl
 
	ld h,0
	ld l, a
	 
	add hl,hl

	ld de, Structure_Graphics_Addresses
	
	
	add hl, de
	
	ld e,(hl)
	inc hl
	ld d,(hl)

	ld ix,0
	add ix,de
	
	pop hl
	inc hl
	

;Store the number of HP the building has, for debug only
	pop af
	push hl
	ld hl, Operation_Center
	or a
	ld de, 23
	sbc hl, de
	ld b,a
	inc b
_
	add hl, de
	djnz -_
	ex de, hl
	inc de
	inc de
	pop hl
	push ix
	push de
	
	pop ix
	ld b, (ix)
 	ld c,(ix+1)
	ld (hl), b
	inc hl
	ld (hl), c
	inc hl
	inc hl
	inc hl
	pop ix

	ld d,0
	ld e, (ix)
	ex de, hl
	add hl, hl
	add hl, hl
	add hl, hl
	ex de, hl

	ld (hl), e  ;Stores Building Width

	inc hl
	ld d, (ix+1)
	ld (hl), d ;Stores Builiding Height



 ld hl, BuildingCount
 inc (hl)
 ret




Draw_Structures_On_Map:
 
 



	ld a, (BuildingCount)
	
	or a
	jp z, Copy_Screen_After_Buildings


	ld ix, (Objects_Address)
	
	dec a
	ld b,a
	inc a
	
 	ld h,0
	ld l,b
 
 	ld d,h
 	ld e,l
 	call multiply_de_by_11

 	ex de, hl
 	ld ix, (Objects_Address)
 
 	add ix, de

Check_If_Structure_In_Range:

	push ix

 	dec a

	ld b, a


 	ld h,0
	ld l,b
 
 	ld d,h
 	ld e,l
 	call multiply_de_by_11

 	ex de, hl
 	ld hl, (Objects_Address)
 
 	add hl, de



	;hl is the X position of the building
	
	

	ld e, (hl)
	inc hl
	ld d, (hl)
	inc hl	
	
	push hl
	
	ex de, hl

	ld de, (MapX)
	or a ;Reset Carry Flag
	sbc hl, de
	push hl


	
    ld    de, 96
    or    a     
    sbc    hl, de
    add    hl, de
    jr    c, Check_Y_Coordinate_In_Range


    ld    e, (ix+9)
	
	
	
	or a
    add    hl, de
	dec hl
    jr    nc, Check_Next_Building


Check_Y_Coordinate_In_Range:
	pop hl
	ld c, l
	pop hl
	
	
	;hl is the Y position of the building
	
	ld e, (hl)
	inc hl
	ld d, (hl)
	inc hl	
	
	
	push hl
	
	ex de, hl

	ld de, (MapY)
	
	or a ;Reset Carry Flag
	sbc hl, de
	push hl



    ld    de, 64
    or    a     
    sbc    hl, de
    add    hl, de
    jr    c, Success_So_Draw_Building


    ld    e, (ix+10)

  
    add    hl, de
	dec hl
    jr    nc, Check_Next_Building



Success_So_Draw_Building:
	
	pop hl
	ld d,l
	
	pop hl
	
	ld a, (hl) ;Register A is the location in the list
	   ;of addresses
 	

	push hl
	ld l,d

;Draws the structure



	push hl ;Save the Y position where the structure
		;is to be drawn

;Grab the right building address
	
	

	
	ld d,0
	ld e, a
	ld h,d
	ld l,e
	add hl,de

	ld de, Structure_Graphics_Addresses
	add hl, de
	
	ld e,(hl)
	inc hl
	ld d,(hl)

	ld ix,0
	add ix,de
	ld a, (ix)
	
	ld (Sprite_Width),a ;Sprite Width
	
	inc ix 
	pop hl ;Retrieve Y position
	
	
	push bc ;Save the number of buildings left to draw
	ld b, (ix) ;Number of Rows
	
	
	inc ix
	push ix
	di
	pop iy
	ld e, (ix)
	ld d, 0
	add iy, de
	inc iy
	
	
	inc ix ;Now IX has the sprite data

	ld a,c ;X Position
	
	ld d,a
	
	ld e,l
	ld a, (Sprite_Width)
	ld c, a

	
	

	
	call LargeClippedSprite
	
	ld iy, flags
	ei
	
	
	
	pop bc
	
	push hl	;To prevent stack crashing
		
	

Check_Next_Building:
	pop hl

	pop hl
	pop ix
	ld de, -11
	add ix, de

		
	ld a,b
	
	or a
	jp nz, Check_If_Structure_In_Range


;==================================================================
;==================================================================
;==================================================================
;==================================================================
;==================================================================


Copy_Screen_After_Buildings:

	BIT Select_Mode, (IY + asm_flag1)
	ret nz
	ld hl, plotsscreen
	call safeCopy
	
	ret

multiply_de_by_11:

 add hl, hl
 add hl, hl
 add hl, hl
 add hl, de
 add hl, de
 add hl, de


 ret

fastCopy:
	di
	ld a,$80
	out ($10),a
	ld hl,plotsscreen-12-(-(12*64)+1)
	ld a,$20
	ld c,a
	inc hl
	dec hl
_
	ld b,64
	inc c
	ld de,-(12*64)+1
	out ($10),a
	add hl,de
	ld de,10
_
	add hl,de
	inc hl
	inc hl
	inc de
	ld a,(hl)
	out ($11),a
	dec de
	djnz -_
	ld a,c
	cp $2B+1
	jr nz,--_
	ei
	ret	
 

